package com.gxa.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxa.common.R;
import com.gxa.pojo.Dict;
import com.gxa.service.DictService;
import com.gxa.service.impl.DictServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/dict/mod")
public class ModDictServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取表单数据
        String dict_name = request.getParameter("dict_name");
        String dict_num = request.getParameter("dict_num");
        String dict_value = request.getParameter("dict_value");
        String remark = request.getParameter("remark");
        int id = Integer.parseInt(request.getParameter("id"));
        //创建Dict实体类
        Dict dict = new Dict();
        dict.setDict_name(dict_name);
        dict.setDict_num(dict_num);
        dict.setDict_value(dict_value);
        dict.setRemark(remark);
        dict.setId(id);
        //获取系统时间
        Date date = new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = format.format(date);

        //调用service
        DictService dictService = new DictServiceImpl();
        R r = null;
        try {
            r = dictService.mod(dict, date1);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //响应jsonResult
        response.setContentType("application/json; charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(r));
    }
}
